package kin.controller.system;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Inject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



import kin.bean.security.Module;
import kin.bean.system.Form;
import kin.context.system.SessionContext;
import kin.manager.security.ModuleManager;
import kin.service.security.ModuleServiceManager;
import kin.system.security.MyInvocationSecurityMetadataSourceService;




import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import kin.test.Tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;





import java.io.IOException;

@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController{
	@Inject
	private ModuleServiceManager moduleServiceManager;
	

	@ResponseBody 
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Object list(HttpSession session) throws Exception {
		/*SecurityContext securityContext = SecurityContextHolder.getContext();
		String role=securityContext.getAuthentication().getPrincipal().toString();
		
		for(GrantedAuthority authority : securityContext.getAuthentication().getAuthorities()){
			if(authority.getAuthority()!=null){
				role=authority.getAuthority();
			}
		}
		Set<String> mc=new HashSet<String>();
		// System.out.println("securityContext:"+securityContext.getAuthentication().getPrincipal());
		Map<String, Collection<ConfigAttribute>> resourceMap =MyInvocationSecurityMetadataSourceService.getResourceMap();
		for (Map.Entry<String, Collection<ConfigAttribute>> entry :resourceMap.entrySet()) {
			 String key = entry.getKey();
			 System.out.println("key:"+key);
			 Collection<ConfigAttribute> cs= resourceMap.get(key);
			for(ConfigAttribute c:cs){
			    System.out.println("c.getAttribute():"+c.getAttribute());
				if(c.getAttribute().equals(role)){
					
					if(!mc.contains(key)){
						mc.add(key);
					}
				}
			}
		 
		}
		
		List<Module> l=this.moduleServiceManager.findParentModuleByRole(mc);*/
		
		List<Module> l=SessionContext.getModule(session.getId());
		/*for(int j=0;j<l.size();j++){
			kin.bean.security.Module mds=(kin.bean.security.Module) l.get(j);
			System.out.println("------------------:"+mds.getModule_id());
			if(mds.getModule_parent()!=null)
			System.out.println("------------------:parent:"+mds.getModule_parent().getModule_id());
		}*/
		
		
		return l;
	}


	public ModuleServiceManager getModuleServiceManager() {
		return moduleServiceManager;
	}


	public void setModuleServiceManager(ModuleServiceManager moduleServiceManager) {
		this.moduleServiceManager = moduleServiceManager;
	}


	
	    
		
		
	
	

	

}
